﻿
using MediaBrowserSL.RIA.Views;
using System;
using MediaBrowserSL.RIA.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace MediaBrowserSL.RIA.MvvmUtils.UI
{
    public class AuthenticationVisualizer : IAuthenticationVisualizer
    {
        private ChildWindowAuthentication dlg;

        public void Show()
        {
            dlg = new ChildWindowAuthentication();
            dlg.LayoutUpdated += this.GoToInitialState;
            EventHandler handler = null;
            handler = delegate
            {
                ((ViewModelBase)dlg.DataContext).RequestClose -= handler;
                dlg.Close();
            };
            ((ViewModelBase)dlg.DataContext).RequestClose += handler;
            dlg.Show();

        }

        private void GoToInitialState(object sender, EventArgs eventArgs)
        {
            if (dlg != null)
            {
                VisualStateManager.GoToState(dlg, "AtFront", false);
                dlg.Title = "Connexion";
                dlg.LayoutUpdated -= this.GoToInitialState;
            }
        }

        public void GoToBack()
        {
            if (dlg != null)
            {
                VisualStateManager.GoToState(dlg, "AtBack", true);
                dlg.Title = "S'inscrire";

            }
        }

        public void GoToFront()
        {
            if (dlg != null)
            {
                VisualStateManager.GoToState(dlg, "AtFront", true);
                dlg.Title = "Connexion";
            }
        }

        public void Refresh()
        {
            if (dlg != null)
            {
                dlg.Focus();
                dlg.TxRegisterLogin.Focus();
            }
        }
        
        public void SetErrorsSummary(List<string> errors)
        {
            if (dlg != null)
            {
                dlg.ValidationSummary.Errors.Clear();
                foreach (var e in errors)
                    dlg.ValidationSummary.Errors.Add(new ValidationSummaryItem(e));
            }
        }
    }
}
